package com.yty.system.entity.vo;

import com.yty.system.entity.enums.ResultEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
public class R<T> implements Serializable {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private String code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    @ApiModelProperty(value = "返回数据")
    private Integer total;

    private R() {}

    //成功静态方法
    public static R ok() {
        R r = new R()
                .setSuccess(true)
                .setCode(ResultEnum.SUCCESS.getCode())
                .setMessage(ResultEnum.SUCCESS.getMessage());
        return r;
    }

    //成功静态方法
    public static R ok(Object data,Integer total) {
        R r = new R()
                .setData(data)
                .setTotal(total)
                .setCode(ResultEnum.SUCCESS.getCode())
                .setMessage(ResultEnum.SUCCESS.getMessage());
        return r;
    }

    //成功静态方法
    public static R ok(Object data) {
        R r = new R()
                .setData(data)
                .setCode(ResultEnum.SUCCESS.getCode())
                .setMessage(ResultEnum.SUCCESS.getMessage());
        return r;
    }

    //失败静态方法
    public static R error() {
        R r = new R()
                .setSuccess(false)
                .setCode(ResultEnum.ERROR.getCode())
                .setMessage(ResultEnum.ERROR.getMessage());
        return r;
    }
    public static R error(String message) {
        R r = new R()
                .setSuccess(false).setMessage(message)
                .setCode(ResultEnum.ERROR.getCode());
        return r;
    }
}

